#include "circleWidget.h"
#include <GC_MakeArcOfCircle.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <TopoDS_Edge.hxx>
#include "BRepPrimAPI_MakePrism.hxx"

circleWidget::circleWidget(modelSoft* myModelSoft, QWidget* parent)
	: myModelSoft(myModelSoft)
	,QWidget(parent)
	, ui(new Ui::circleWidgetClass())
{
	ui->setupUi(this);
	ui->confirmButton->setEnabled(false);
	ui->modifyButton->setEnabled(false);
	//建立直线拉伸成平面
	gp_Pnt pos1(-1, -1, 0);
	gp_Pnt pos2(1, -1, 0);
	BRepBuilderAPI_MakeEdge edgeShape(pos1, pos2);
	gp_Vec stretchDirection(0, 2, 0);
	const TopoDS_Shape& newShape = BRepPrimAPI_MakePrism(edgeShape, stretchDirection);
	stretchPipeline = myModelSoft->CreatePipeline(newShape, -1, false);
	myModelSoft->myView->getRenderWindow()->Render();
	CreateConnections();

}

circleWidget::~circleWidget()
{
	delete ui;
}

void circleWidget::receiveVertexPoint(gp_Pnt position)
{
	vertexPos[receiveVertexNum][0] = position.X();
	vertexPos[receiveVertexNum][1] = position.Y();
	vertexPos[receiveVertexNum][2] = position.Z();
	//可视化接收点
	vertexActors.push_back(myModelSoft->CreateTempSphereActor(0.02f, gp_Pnt(vertexPos[receiveVertexNum][0], vertexPos[receiveVertexNum][1], vertexPos[receiveVertexNum][2])));
	receiveVertexNum++;
	if(receiveVertexNum == 1) ui->modifyButton->setEnabled(true);
}

void circleWidget::closeEvent(QCloseEvent* event)
{
	// 没有点确定，删除创建的Actor
	if (!confirmCreated)
	{
		if (!resultPipeline.IsNull())
		{
			myModelSoft->RemovePipelineFromRenderer(resultPipeline);
			myModelSoft->myView->getRenderWindow()->Render();
		}
		for (int i = 0; i < receiveVertexNum; i++) {
			myModelSoft->myView->getRenderer()->RemoveActor(vertexActors[i]);
		}
		myModelSoft->myView->getRenderWindow()->Render();
	}
	myModelSoft->RemovePipelineFromRenderer(stretchPipeline);
	close();

}

void circleWidget::OnConfirm()
{
	confirmCreated = true;
	for (int i = 0; i < receiveVertexNum; i++) {
		myModelSoft->myView->getRenderer()->RemoveActor(vertexActors[i]);
	}
	int row = myModelSoft->ui->model->rowCount();
	QStandardItem* item1 = new QStandardItem(QString("圆-%1").arg(resultPipeline->GetShapeID()));
	item1->setEditable(false);
	myModelSoft->ui->model->setItem(row, 0, item1);
	QStandardItem* item2 = new QStandardItem(QString("隐藏"));
	item2->setCheckable(true);
	myModelSoft->ui->model->setItem(row, 1, item2);
	close();

}

void circleWidget::OnModify()
{
	if (modifyStatus) {
		return;
	}
	gp_Pnt center(vertexPos[0][0], vertexPos[0][1], vertexPos[0][2]); // 圆心
	gp_Dir axis(0, 0, 1);  // 圆所在平面的法线方向
	double radius = ui->curvatureInput->text().toDouble();  // 半径

	gp_Ax2 ax2(center, axis); // 定义圆的平面和位置
	gp_Circ circle(ax2, radius); // 创建圆
	//转换成TopoDS_Shape对象
    TopoDS_Shape circleShape = BRepBuilderAPI_MakeEdge(circle).Shape();
	resultPipeline = myModelSoft->CreatePipeline(circleShape, -1, false);
	resultPipeline->Actor()->GetProperty()->SetLineWidth(3);
	modifyStatus = true;
	ui->confirmButton->setEnabled(true);
	myModelSoft->myView->getRenderWindow()->Render();

}

void circleWidget::OnSelVertex()
{
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Vertex);
	myModelSoft->changeSelector(SM_Vertex);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);
	myModelSoft->myView->getInteractorStyle()->setCurveOperation(true);

}

void circleWidget::OnCancelSelVertex()
{
}

void circleWidget::CreateConnections()
{
	connect(myModelSoft->currVertexSelector, &OccVertexSelector::leftButtonDown, this, &circleWidget::receiveVertexPoint);
	connect(ui->cancelButton, &QPushButton::clicked, this, &circleWidget::close);
	connect(ui->confirmButton, &QPushButton::clicked, this, &circleWidget::OnConfirm);
	connect(ui->modifyButton, &QPushButton::clicked, this, &circleWidget::OnModify);
	connect(ui->vertexCancelSelect, &QPushButton::clicked, this, &circleWidget::OnCancelSelVertex);
	connect(ui->vertexSelect, &QPushButton::clicked, this, &circleWidget::OnSelVertex);

}
